// Copyright (C) 2021, Flickering Inc. All rights reserved.
// Author: haiquanchen (haiquanchen@flickering.ai)

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property({ type: Number, displayName: '速度' })
    speed: number = 1;
    private _time: number = 0;
    private _material: cc.MaterialVariant;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        if (this.node.getComponent(cc.Label)) {
            this._material = this.node.getComponent(cc.Label).getMaterial(0);
        } else if (this.node.getComponent(cc.Sprite)) {
            this._material = this.node.getComponent(cc.Sprite).getMaterial(0);
        }
    }

    start() {}

    update(dt) {
        if (this._material) {
            if (this._time > 1) {
                this._time = 0;
            }

            this._material.setProperty('u_time', this._time);
            this._time += dt * this.speed;
        }
    }
}
